/**
 * 
 */
package com.nqc.perodictable;

import com.nqc.perodictable.config.ReadConfig;
import com.nqc.perodictable.device.Device;
import com.nqc.perodicttable.R;

import android.app.Activity;
import android.content.Context;
import android.graphics.Color;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.TableRow.LayoutParams;

/**
 * @author nguyenquocchinh
 *
 */
public class PerodicTable extends Activity{
	
	private Block blocks[][]; // blocks for mine field
	private int blockDimension = 50; // width of each block
	private int blockPadding = 0; // padding between blocks

	private int numberOfRowsInMineField = 18;
	private int numberOfColumnsInMineField = 7;

	private TableLayout mineField;
	
	private View element;
	private LayoutInflater layoutInflate;
	
	
	private ReadConfig readConfig;
	private String[] config;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		
		setContentView(R.layout.grid_game);
		
		
		mineField = (TableLayout) findViewById(R.id.MineField);
		blocks = new Block[numberOfRowsInMineField][numberOfColumnsInMineField];
		element = ((LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.chemical_element, null, false);
		readConfig = new ReadConfig();
		config = readConfig.readFromAssest(getApplicationContext());
		
		showMineField();
	}
	
	private void showMineField() {
		for (int row = 0; row < numberOfRowsInMineField; row++) {
			TableRow tableRow = new TableRow(this);
			tableRow.setWeightSum(7);
			tableRow.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT));
			tableRow.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL | Gravity.CENTER);
			for (int column = 0; column < numberOfColumnsInMineField; column++) {

				 blocks[row][column] = new Block(this);
				 blocks[row][column].setLayoutParams(new LayoutParams(Device.getScreenSize(this)[0] / 7, Device.getScreenSize(this)[1] / 18));
				 blocks[row][column].setPadding(blockPadding, blockPadding, blockPadding, blockPadding);
				
				tableRow.addView(blocks[row][column]);
				
				blocks[row][column].setText(row +""+ column);
				setLantanAndActini(row, column, blocks[row][column]);

			}
			
//			mineField.addView(tableRow, new TableLayout.LayoutParams(
//					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//			mineField.setGravity(Gravity.CENTER_HORIZONTAL
//					| Gravity.CENTER_VERTICAL | Gravity.CENTER);
			
			mineField.addView(tableRow, new TableLayout.LayoutParams(Device.getScreenSize(this)[0], Device.getScreenSize(this)[1]));
			mineField.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL | Gravity.CENTER);
		}
		
		setColorElement();
	}
	
	public void setLantanAndActini(int row, int column, Button button)
	{
		if(row == 3 && column == 0)
			button.setText("**");
		if(row == 3 && column == 1)
			button.setText("*");
	}
	
	public void setColor(int row,int col, int stat, int end, Button button, int color)
	{
		if (row == 2 && col == 6)
		for(int i = row; i < end; i++){
			button.setBackgroundColor(color);
		}
	}
	
	private void setColorElement()
	{
		
		for(int i = 0 ; i< numberOfRowsInMineField; i++)
		{
			String[] split = config[i].split("\\.");
			for(int j = 0; j < numberOfColumnsInMineField; j++)
			{
				if(split[j].equals("g")){
					blocks[i][j].setBackgroundColor(Color.GREEN);					
				}
				if(split[j].equals("y")){
					blocks[i][j].setBackgroundColor(Color.YELLOW);					
				}
				if(split[j].equals("m")){
					blocks[i][j].setBackgroundColor(Color.MAGENTA);					
				}
				if(split[j].equals("w")){
					blocks[i][j].setBackgroundColor(Color.TRANSPARENT);					
				}

			}
		}
			
	}
	
}
